class Stack {
    constructor () {
        this.count = 0 // count记录栈的大小
        this.items = {}
    }
    push (element) {
        this.items[this.count] = element
        this.count++
    }
    // 验证一个栈是否为空
    isEmpty () {
        return this.count === 0
    }
    // 查看一个栈的大小
    size () {
        return this.count
    }
    // 从栈中弹出元素
    pop () {
        // 检查栈是否为空
        if (this.isEmpty()) {
            return undefined
        }
        this.count--
        const result = this.items[this.count]
        delete this.items[this.count]
        return result
    }
    //查看栈顶的值 并将栈清空
    peek () {
        if (this.isEmpty()) {
            return undefined
        }
        return this.items[this.count - 1]
    }
    // clear () {
    //     this.count = 0
    //     this.items = {}
    // }
    // 遵循后进先出原则
    clear () {
        while (this.isEmpty()) {
            this.pop()
        }
    }
}
const stack = new Stack()
stack.push('q')
stack.push('i')
stack.push('n')
console.log(stack)
console.log(stack.peek());